import requests

# # 定义循环变量
# while True:
#     # 输出提示信息
#     print("请输入要查询的QQ号码：")
#     # 获取用户输入
#     qq = input("是否继续查询？(0退出，1查询)")
#     # 判断用户输入是否退出
#     if qq.lower() == "0":
#         break
#     # 构造查询URL
#     callurl = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode="
#     # 构造请求参数
#     params = f"qqCode={qq}"
#     # 发送GET请求
#     response = requests.get(callurl + params)
#     # 获取返回结果
#     result = response.text.strip()
#     # 输出结果
#     print("当前QQ号码：", result)







# import urllib.request
# while True:
#     print("请输入要查询的QQ号码、输入“0”退出")
#     qq = input()
#     if qq == '0':
#         break
#     result = ''
#     charset = 'utf-8'
#     callurl = 'http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=' + qq
#     try:
#         with urllib.request.urlopen(callurl) as response:
#             html = response.read()
#             result = html.decode(charset)
#     except Exception as e:
#         print(e)
#     len = result.find('\">')
#     if len != -1:
#         qStruts = result[len+2:len+3]
#         if qStruts == 'E':
#             print('QQ号码错误')
#         elif qStruts == 'Y':
#             print('在线')
#         elif qStruts == 'N':
#             print('离线')
#     else:
#         print('服务器繁忙，请从试!')





import urllib.request, csv

results = []

with open('/Users/apple/gitee/think-for-yourself/02--爬虫学习笔记/06--bs4/qqnumberresults.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        qq = row[0]
        result = ''
        charset = 'utf-8'
        callurl = 'http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=' + qq
        try:
            with urllib.request.urlopen(callurl) as response:
                html = response.read()
                result = html.decode(charset)
        except Exception as e:
            print(e)
        len = result.find('\">')
        if len != -1:
            qStruts = result[len+2:len+3]
            if qStruts == 'E':
                results.append((qq, 'QQ号码错误'))
            elif qStruts == 'Y':
                results.append((qq, '在线'))
            elif qStruts == 'N':
                results.append((qq, '离线'))
        else:
            results.append((qq, '服务器繁忙，请重试！'))

with open('/Users/apple/gitee/think-for-yourself/02--爬虫学习笔记/06--bs4/qqnumberresults.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(results)

print('批量判断QQ号码完成！')